/*
 * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.liu.common.commons;

import com.liu.common.enums.HttpStatus;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author lengleng
 */
public class R<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	private int code;

	private String msg;

	private T data;

	public static <T> R<T> ok() {
		return restResult(null, HttpStatus.OK.value(), "成功");
	}

	public static <T> R<T> ok(T data) {
		return restResult(data, HttpStatus.OK.value(), "成功");
	}
	public static <T> R<T> ok(int code, String msg) {
		return restResult(null, code, msg);
	}

	public static <T> R<T> ok(T data, String msg) {
		return restResult(data, HttpStatus.OK.value(), msg);
	}

	public static <T> R<T> ok(T data,HttpStatus httpStatus) {
		return restResult(data, httpStatus.value(), httpStatus.msg());
	}

	public static <T> R<T> failed() {
		return restResult(null, HttpStatus.INTERNAL_SERVER_ERROR.value(), "失败");
	}

	public static <T> R<T> failed(String msg) {
		return restResult(null, HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
	}
	public static <T> R<T> failed(int code,String msg) {
		return restResult(null, code, msg);
	}

	public static <T> R<T> failed(T data) {
		return restResult(data, HttpStatus.INTERNAL_SERVER_ERROR.value(), "失败");
	}

	public static <T> R<T> failed(T data, String msg) {
		return restResult(data, HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
	}
	public static <T> R<T> failed(T data,int code, String msg) {
		return restResult(data, code, msg);
	}
	public static <T> R<T> failed(T data,HttpStatus httpStatus) {
		return restResult(data, httpStatus.value(), httpStatus.msg());
	}

	private static <T> R<T> restResult(T data, int code, String msg) {
		R<T> apiResult = new R<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);
		return apiResult;
	}


	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	@Override
	public String toString() {
		return "R{" +
				"code=" + code +
				", msg='" + msg + '\'' +
				", data=" + data +
				'}';
	}


}
